<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16
 * Time: 17:30
 */
namespace app\install\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Jump;
/**
 * ----------------------------------
 * Class Install
 * @package app\install\controller
 * ----------------------------------
 */
class Install extends Controller{
    /**
     * 构造函数
     */
    protected function _initialize(){
        $foenp = fopen("./lock.txt","a+");
        $content = file_get_contents("./lock.txt");
        if(!empty($content)){
            $msg = '已经成功安装了phpbloger，请不要重复安装!';
            $this->error($msg,url('admin/login/login'));
        }
    }
    /**
     * @return \think\response\View
     * 环境检测
     */
    public function step1(){
        session('error', false);
        //环境检测
        $env = check_env();
        //目录文件读写检测
        $dirfile = check_dirfile();
        //函数检测
        $func = check_func();
        session('step', 1);
        return view('step1',['env'=>$env,'dirfile'=>$dirfile,'func'=>$func]);
    }

    /**
     * @return \think\response\View
     * 开始创建数据库
     */
    public function step2(){
        if(Request::instance()->isPost()){
            //检测管理员信息
           $post = Request::instance()->post();
            if(!is_array($post['admin']) || empty($post['admin']['0']) || empty($post['admin']['1']) || empty($post['admin']['2'])){
                $this->error('请填写完整管理员信息');
            }else if($post['admin'][1] != $post['admin'][2]){
                $this->error('确认密码和密码不一致');
            } else {
                $info = array();
                list($info['username'], $info['password'], $info['repassword']) = $post['admin'];
                session('admin_info', $info); //缓存管理员信息
            }
            //检测数据库信息
            if(!is_array($post['db']) || empty($post['db'][0]) || empty($post['db'][2]) || empty($post['db'][3]) || empty($post['db'][4])){
                $this->error('请填写完整的数据库配置');//检测数据库配置
            }else {
                $DB = array();
                list($DB['type'],$DB['dsn'], $DB['hostname'], $DB['database'], $DB['username'], $DB['password'], $DB['hostport'], $DB['params'],$DB['charset'],$DB['prefix']) = $post['db'];
                session('db_config', $DB);  //缓存数据库配置
                //创建数据库
                $db_name = $DB['database'];
                unset($DB['database']);
                $db = Db::connect($DB['type'].'://'.$DB['username'].':'.$DB['password'].'@'.$DB['hostname'].':'.$DB['hostport']);
                $sql = "CREATE DATABASE IF NOT EXISTS `{$db_name}` DEFAULT CHARACTER SET utf8";
                $db->execute($sql) || $this->error($db->getError());
            }
            //跳转到数据库安装页面
            $this->redirect('step3');
        } else {
            session('error') && $this->error('环境检测没有通过，请调整环境后重试！');
            $step = session('step');
            if($step != 1 && $step != 2){
                $this->redirect('step1');
            }
            session('step', 2);
            return view('step2');
        }
    }

    /**
     *导入数据库
     */
    public function step3(){
        if(session('step') != 2){
            $this->redirect('step2');
        }
        echo $this->fetch();
        $db_config = session('db_config');
        $db = Db::connect($db_config);//连接数据库
        create_tables($db, $db_config['prefix']); //创建数据表
        $admin = session('admin_info');
        register_administrator($db, $db_config['prefix'],$admin); //创建管理员帐号
        //修改配置文件
        $conf   =   write_config($db_config);
        session('config_file',$conf);
        if(session('error')){
            show_msg('安装错误');
        } else {
            session('step', 3);
            $url = 'http://'.$_SERVER['HTTP_HOST'].'/install/index/complete';
            echo "<script>window.location.href='".$url."';</script> ";

        }
    }
}